//
//  MineHeaderView.m
//  Peso Guide
//
//  Created by macbook on 2025/6/12.
//

#import "MineHeaderView.h"
#import "MineSettingViewController.h"

@interface MineHeaderView ()

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIImageView *topImg;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *welcomLabel;
@property (nonatomic, strong) UIButton *settingButton;

@end

@implementation MineHeaderView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = ColorHex(@"#1A7A35");
        
        _backView = [PesoKit createViewWithFrame:CGRectMake(0, 0, KScreenWidth, 120) backGroundColor:ColorHex(@"#1A7A35")];
        [self addSubview:_backView];
        
        _bottomView = [PesoKit createViewWithFrame:CGRectMake(0, 120, KScreenWidth, 30) backGroundColor:ColorHex(@"#FFFFFF")];
        _bottomView.clipsToBounds = YES;
        _bottomView.layer.mask = [AppTools clipTopRightAndLeftByRoundingCorners:_bottomView radii:CGSizeMake(15.0f, 15.0f)];
        [self addSubview:_bottomView];
        
        _topImg = [PesoKit createImageViewWithFrame:CGRectMake(24, 4, 88, 88) imageNamed:@"ic_mine_avatar"];
        _topImg.clipsToBounds = YES;
        _topImg.layer.cornerRadius = 44.0f;
        [_backView addSubview:_topImg];
        
        _phoneLabel = [PesoKit createLabelWithFrame:CGRectMake(124, 24, KScreenWidth, 22) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleMedium, 16)];
        [_backView addSubview:_phoneLabel];
        
        _welcomLabel = [PesoKit createLabelWithFrame:CGRectMake(124, 55, KScreenWidth, 17) backGroundColor:[UIColor clearColor] text:@"Welcome to Peso Guide" textColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleRegular, 12)];
        [_backView addSubview:_welcomLabel];
        
        _settingButton = [[UIButton alloc] initWithFrame:CGRectMake(KScreenWidth-24-20, 33, 20, 20)];
        [_settingButton addTarget:self action:@selector(settingButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_settingButton setBackgroundImage:[UIImage imageNamed:@"ic_mine_setting"] forState:0];
        [_backView addSubview:_settingButton];
        
        UIButton *setButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 120)];
        setButton.backgroundColor = [UIColor clearColor];
        [setButton addTarget:self action:@selector(settingButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_backView addSubview:setButton];

    }
    return self;
}

- (void)refreshUserInfo {
    NSString *userEnterPhone = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user_phone"]];
    NSString *user_phone_start = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"user_phone_start"]];
    
    self.phoneLabel.text = [self formatPhoneNumber:[NSString stringWithFormat:@"%@%@",user_phone_start,userEnterPhone]];
}

- (void)settingButtonClick {
    MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
    settingVC.hidesBottomBarWhenPushed = YES;
    [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
}

- (NSString *)formatPhoneNumber:(NSString *)phoneNumber {
    // 1. 验证输入有效性
    if (!phoneNumber || phoneNumber.length < 6) {
        return phoneNumber; // 返回原值或自定义处理
    }
    
    // 2. 提取前两位
    NSString *prefix = [phoneNumber substringToIndex:2];
    
    // 3. 提取后四位
    NSString *suffix = [phoneNumber substringFromIndex:phoneNumber.length - 4];
    
    // 4. 创建星号部分（中间部分）
    NSInteger starCount = phoneNumber.length - 6; // 计算需要显示的星号数量
    NSMutableString *stars = [NSMutableString string];
    for (NSInteger i = 0; i < starCount; i++) {
        [stars appendString:@"*"];
    }
    
    // 5. 组合最终字符串
    return [NSString stringWithFormat:@"%@%@%@", prefix, stars, suffix];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
